package com.night.card.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.night.card.mapper.CardOutMachineMapper;
import com.night.card.service.interfs.CadOutMachineService;
import com.night.common.entity.card.CardOutMachine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CardOutMachineServiceImp
 * @Description: 消费卡机service层实现
 * @author: CHX42
 * @date: 2022/10/21-9:27
 * @version: v1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CardOutMachineServiceImp implements CadOutMachineService {

    @Value("${ss.card.pageSize}")
    private int pageSize;

    @Resource
    private CardOutMachineMapper cardOutMachineMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void add(CardOutMachine cardOutMachine) {
        cardOutMachineMapper.insert(cardOutMachine);
    }

    @Override
    public void update(CardOutMachine cardOutMachine) {
        cardOutMachineMapper.update(cardOutMachine);
    }

    @Override
    public CardOutMachine getById(Integer id) {
        String carOutMachineName = "card_out_machine_" + id;
        CardOutMachine redisCardOutMachine = (CardOutMachine) redisTemplate.opsForValue().get(carOutMachineName);
        if (redisCardOutMachine != null)
            return redisCardOutMachine;
        CardOutMachine cardOutMachine = cardOutMachineMapper.selectById(id);
        redisTemplate.opsForValue().set(carOutMachineName, cardOutMachine, 30, TimeUnit.SECONDS);
        return cardOutMachine;
    }

    @Override
    public PageInfo<CardOutMachine> getAll() {
        String pageName = "card_out_machine_all";
        List<CardOutMachine> redisCardOutMachineList = (List<CardOutMachine>) redisTemplate.opsForValue().get(pageName);
        if (redisCardOutMachineList != null)
            return new PageInfo<>(redisCardOutMachineList);
        List<CardOutMachine> cardOutMachineList = cardOutMachineMapper.select(new CardOutMachine());
        redisTemplate.opsForValue().set(pageName, cardOutMachineList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardOutMachineList);
    }

    @Override
    public PageInfo<CardOutMachine> getBy(PageInfo<CardOutMachine> pageInfo) {
        String pageName = "card_out_machine_list_" + pageInfo.getPageNum() + "_" + pageInfo.getPageSize();
        PageHelper.startPage(pageInfo.getPageNum(), pageSize);
        List<CardOutMachine> redisCardOutMachineList = (List<CardOutMachine>) redisTemplate.opsForValue().get(pageName);
        if (redisCardOutMachineList != null)
            return new PageInfo<>(redisCardOutMachineList);
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<CardOutMachine> cardOutMachineList = cardOutMachineMapper.select(pageInfo.getList().get(0));
        //写入数据库
        redisTemplate.opsForValue().set(pageName, cardOutMachineList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardOutMachineList);
    }
}
